package com.mb.ext.core.dao.impl.product;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.NoResultException;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.mb.ext.core.dao.AbstractBaseDAO;
import com.mb.ext.core.dao.product.ProductFreightDAO;
import com.mb.ext.core.entity.merchant.MerchantEntity;
import com.mb.ext.core.entity.product.ProductFreightEntity;
import com.mb.framework.exception.DAOException;
import com.mb.framework.util.log.LogHelper;

@Repository("productFreightDAO")
@Qualifier("productFreightDAO")
public class ProductFreightDAOImpl extends AbstractBaseDAO<ProductFreightEntity> implements ProductFreightDAO
{
	private final LogHelper logger = LogHelper.getInstance(this.getClass().getName());

	/**
	 * Initializing Entity.
	 */
	public ProductFreightDAOImpl()
	{
		super();
		this.setEntityClass(ProductFreightEntity.class);
	}

	@Override
	public void createProductFreight(ProductFreightEntity productFreightEntity)
			throws DAOException {
		save(productFreightEntity);
		
	}

	@Override
	public void updateProductFreight(ProductFreightEntity productFreightEntity)
			throws DAOException {
		update(productFreightEntity);
		
	}

	@Override
	public void deleteProductFreight(ProductFreightEntity productFreightEntity)
			throws DAOException {
		delete(productFreightEntity);
		
	}

	@Override
	public List<ProductFreightEntity> getFreights() throws DAOException {
		List<ProductFreightEntity> freightEntityList = new ArrayList<ProductFreightEntity>();;
		try {
			freightEntityList = em.createQuery("select b from ProductFreightEntity b where  b.isDeleted=:isDeleted",ProductFreightEntity.class).setParameter("isDeleted", Boolean.FALSE).getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for supplier industry");
		}
		return freightEntityList;
	}
	
	@Override
	public List<ProductFreightEntity> getFreightsByMerchant(MerchantEntity merchantEntity) throws DAOException {
		List<ProductFreightEntity> freightEntityList = new ArrayList<ProductFreightEntity>();;
		try {
			freightEntityList = em.createQuery("select b from ProductFreightEntity b where b.merchantEntity.merchantUuid = :MERCHANTUUID and b.isDeleted=:isDeleted",ProductFreightEntity.class)
					.setParameter("isDeleted", Boolean.FALSE)
					.setParameter("MERCHANTUUID", merchantEntity.getMerchantUuid())
					.getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for supplier industry");
		}
		return freightEntityList;
	}

	@Override
	public ProductFreightEntity getFreightByUuid(String uuid) throws DAOException {
		ProductFreightEntity freightEntity = new ProductFreightEntity();
		try {
			freightEntity = (ProductFreightEntity)em.createQuery("select b from ProductFreightEntity b where b.productFreightUuid = :UUID",ProductFreightEntity.class).setParameter("UUID", uuid).getSingleResult();
		} catch (NoResultException e) {
			logger.info("No record found for supplier industry");
		}
		return freightEntity;
	}

}
